package com.sy.simpleegg.room.repository

import com.sy.simpleegg.constant.PackageType
import com.sy.simpleegg.constant.PayState
import com.sy.simpleegg.constant.PayType
import com.sy.simpleegg.platform.BackstagePlatform
import com.sy.simpleegg.room.dao.OrderDao
import com.sy.simpleegg.room.entity.OrderEntity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class OrderRepository(private val orderDao: OrderDao) {

    suspend fun getAll(): List<OrderEntity>?{
        return withContext(Dispatchers.IO){
            orderDao.getAll()
        }
    }

    suspend fun getByDate(date: String): MutableList<OrderEntity>?{
        return withContext(Dispatchers.IO){
            orderDao.getByDate(date)
        }
    }

    suspend fun getByUuid(uuid: String): List<OrderEntity>?{
        return withContext(Dispatchers.IO){
            orderDao.getByUuidd(uuid)
        }
    }

    suspend fun getByOrderNumber(orderNumber: String): List<OrderEntity>?{
        return withContext(Dispatchers.IO){
            orderDao.getByOrderNumber(orderNumber)
        }
    }

    suspend fun getByPayUUID(payUUID: String): List<OrderEntity>?{
        return withContext(Dispatchers.IO){
            orderDao.getByPayUUID(payUUID)
        }
    }

    suspend fun getLastCashOrder(platform: BackstagePlatform): MutableList<OrderEntity>?{
        return withContext(Dispatchers.IO){
            orderDao.getLastOrder(platform, PackageType.CASH, PayState.UNPAYED)
        }
    }

    suspend fun getLastNormalOrder(platform: BackstagePlatform): MutableList<OrderEntity>?{
        return withContext(Dispatchers.IO){
            orderDao.getLastOrder(platform, PackageType.NORMAL, PayState.UNPAYED)
        }
    }

    suspend fun getLastNormalAmountOrder(platform: BackstagePlatform, coins: String): MutableList<OrderEntity>?{
        return withContext(Dispatchers.IO){
            orderDao.getLastOrder(platform, PackageType.NORMAL, PayState.UNPAYED, coins)
        }
    }

    suspend fun getLastRechargeOrder(platform: BackstagePlatform): MutableList<OrderEntity>?{
        return withContext(Dispatchers.IO){
            orderDao.getLastOrder(platform, PackageType.RECHARGE, PayState.SUCCESS)
        }
    }

    suspend fun update(vararg orderEntity: OrderEntity){
        withContext(Dispatchers.IO){
            orderDao.updateTransition(*orderEntity)
        }
    }

    suspend fun updateByUuid(uuid: String, payState: PayState){
        withContext(Dispatchers.IO){
            orderDao.updateByUuid(uuid, payState)
        }
    }

    suspend fun updatePayType(uuid: String, payType: PayType){
        withContext(Dispatchers.IO){
            orderDao.updatePayType(uuid, payType)
        }
    }

    suspend fun insert(orderEntity: OrderEntity){
        withContext(Dispatchers.IO){
            orderDao.insert(orderEntity)
        }
    }

    suspend fun insertAll(orderEntities: List<OrderEntity>){
        withContext(Dispatchers.IO){
            orderDao.insertAll(orderEntities)
        }
    }

    suspend fun delete(orderEntity: OrderEntity){
        withContext(Dispatchers.IO){
            orderDao.delete(orderEntity)
        }
    }

    suspend fun deleteAll(){
        withContext(Dispatchers.IO){
            orderDao.deleteAll()
        }
    }

    suspend fun deleteByUuid(uuid: String){
        withContext(Dispatchers.IO){
            orderDao.deleteByUuid(uuid)
        }
    }

    suspend fun deleteByOrderNumber(orderNumber: String){
        withContext(Dispatchers.IO){
            orderDao.deleteByOrderNumber(orderNumber)
        }
    }

    suspend fun updateRealCoins(orderNumber: String, realCoins: String){
        withContext(Dispatchers.IO){
            orderDao.updateRealCoins(orderNumber, realCoins)
        }
    }

    suspend fun getOrderByOrderNumber(orderNumber: String, payType: PayType): MutableList<OrderEntity>?{
        return withContext(Dispatchers.IO){
            orderDao.getByOrderNumber(orderNumber, payType)
        }
    }

    suspend fun getOrderSupplement(startData: String, endDate: String, payState: PayState): MutableList<OrderEntity>?{
        return withContext(Dispatchers.IO){
            orderDao.getSupplement(startData, endDate, payState)
        }
    }

    suspend fun setRealAmount(id: Long, realAmount: String){
        withContext(Dispatchers.IO){
            orderDao.setRealAmount(id, realAmount)
        }
    }

    suspend fun verifyOrderNumber(platform: BackstagePlatform, orderNumber: String): Int{
        return withContext(Dispatchers.IO){
            orderDao.verifyOrderNumber(platform, orderNumber)
        }
    }

}